#include "customvertex.h"
#include "cube.h"
#include "string.h"

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)

#define  NUM_VERTS	12
#define  BOTTOMHALFOFFSET 6

static int CubeIDCounter = 1;

	CUSTOMVERTEX g_Vertices[] =
	{
		{ 0.0f, 1.41f, 0.0f,  0.0f,0.0f },	// top
		{ 1.0f, 0.0f, 1.0f,  1.0f,0.0f },	// beginning
		{ 1.0f, 0.0f,-1.0f,  1.0f,0.0f },
		{-1.0f, 0.0f,-1.0f,  1.0f,1.0f },
		{-1.0f, 0.0f, 1.0f,  0.0f,1.0f },
		{ 1.0f, 0.0f, 1.0f,  1.0f,0.0f },	// beginning
		
		{ 0.0f,-1.41f, 0.0f,  0.0f,0.0f },	// bottom
		{ 1.0f, 0.0f, 1.0f,  1.0f,0.0f },	// beginning
		{-1.0f, 0.0f, 1.0f,  0.0f,1.0f },
		{-1.0f, 0.0f,-1.0f,  1.0f,1.0f },
		{ 1.0f, 0.0f,-1.0f,  1.0f,0.0f },
		{ 1.0f, 0.0f, 1.0f,  1.0f,0.0f },	// beginning
	};




int  cube::setupVB(CUSTOMVERTEX *v_buff,int count)
{
	ID = CubeIDCounter;
	CubeIDCounter++;

	forceY = 0.0;
	forceX = 0.0;

	vi_Y = 0.0;
	vi_X = 0.0;

	if(ID == 1)
	{
		transY = 8;
		transX = 0.0;
	}
	else if(ID == 2)
	{
		transY = 4;
		transX = 2.0;
	}
	else if(ID == 3)
	{
		transY = 8;
		transX = 15.0;
	}
	else if(ID == 4)
	{
		transY = 14;
		transX = 29.0;
	}
	

    memcpy( v_buff, g_Vertices, sizeof(CUSTOMVERTEX)*NUM_VERTS );
	mycount = count;
	return count+NUM_VERTS;
}

void  cube::draw(LPDIRECT3DDEVICE9 *pd3dDevice, LPDIRECT3DTEXTURE9 * g_pTexture2)
{
	(*pd3dDevice)->SetFVF( D3DFVF_CUSTOMVERTEX );
	
    (*pd3dDevice)->DrawPrimitive( D3DPT_TRIANGLEFAN,  mycount, 4 );
	(*pd3dDevice)->SetTexture( 0, *g_pTexture2 );
	(*pd3dDevice)->DrawPrimitive( D3DPT_TRIANGLEFAN,  mycount+BOTTOMHALFOFFSET, 4 );
	
}

void cube::update()
{
	float gravity = -0.00490;

	float time_unit = 1;

	float vm_Y = 0.24;
	float vm_X = 0.05;

	if(transY > 0)
	{
		//transY -= 0.1;
		if(vi_Y > vm_Y)
		{
			vi_Y = vm_Y;
		}
		transY += vi_Y + 0.5 * (forceY + gravity) * time_unit;
		vi_Y = vi_Y + gravity * time_unit;
		
	}
	else
	{
		
		vi_Y *= -1;
		transY += vi_Y + 0.5 * (forceY + gravity) * time_unit;
		vi_Y = vi_Y + gravity * time_unit;

		if(transY < 0)
		{
			transY = 0;
		}
	}


}